package com.xframework.gateway.config;

import com.xframework.gateway.handler.GatewayExceptionHandler;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.web.reactive.result.view.ViewResolver;

import java.util.Collections;
import java.util.List;

@Configuration
public class GatewayConfig {
    @Primary
    @Bean
    @Order(Ordered.HIGHEST_PRECEDENCE)
    public ErrorWebExceptionHandler errorWebExceptionHandler(ObjectProvider<List<ViewResolver>> viewResolversProvider, ServerCodecConfigurer serverCodecConfigurer) {
        System.out.println("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx GatewayExceptionHandler xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        GatewayExceptionHandler gatewayExceptionHandler = new GatewayExceptionHandler();
        gatewayExceptionHandler.setViewResolvers(viewResolversProvider.getIfAvailable(Collections::emptyList));
        gatewayExceptionHandler.setMessageWriters(serverCodecConfigurer.getWriters());
        gatewayExceptionHandler.setMessageReaders(serverCodecConfigurer.getReaders());
        return gatewayExceptionHandler;
    }

//    public static final long DEFAULT_TIMEOUT = 30000;
//
//    public static String NACOS_SERVER_ADDR;
//    public static String NACOS_NAMESPACE;
//    public static String NACOS_ROUTE_DATA_ID;
//    public static String NACOS_ROUTE_GROUP;
//
//    @Value("${spring.cloud.nacos.discovery.server-addr}")
//    public void setNacosServerAddr(String nacosServerAddr){
//        NACOS_SERVER_ADDR = nacosServerAddr;
//    }
//
//    @Value("${spring.cloud.nacos.discovery.namespace}")
//    public void setNacosNamespace(String nacosNamespace){
//        NACOS_NAMESPACE = nacosNamespace;
//    }
//
//    @Value("${nacos.gateway.route.config.data-id}")
//    public void setNacosRouteDataId(String nacosRouteDataId){
//        NACOS_ROUTE_DATA_ID = nacosRouteDataId;
//    }
//
//    @Value("${nacos.gateway.route.config.group}")
//    public void setNacosRouteGroup(String nacosRouteGroup){
//        NACOS_ROUTE_GROUP = nacosRouteGroup;
//    }
}
